package com.cskaoyan.config;

import com.cskaoyan.converter.String2DateConverter;
import com.cskaoyan.interceptor.CustomHandlerInterceptor;
import com.cskaoyan.interceptor.CustomHandlerInterceptor2;
import com.cskaoyan.interceptor.CustomHandlerInterceptor3;
import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.format.FormatterRegistry;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;

/**
 * SpringMVC配置类
 * @author stone
 * @date 2022/01/04 09:54
 */
@ComponentScan("com.cskaoyan.controller")
@EnableWebMvc
public class MvcConfiguration implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //静态资源映射mapping location
        registry.addResourceHandler("/pic1/**").addResourceLocations("/");
        registry.addResourceHandler("/pic2/**").addResourceLocations("classpath:/");
        registry.addResourceHandler("/pic3/**").addResourceLocations("file:d:/spring/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //HandlerInterceptor是谁、作用范围是什么、顺序
        registry.addInterceptor(new CustomHandlerInterceptor());//作用范围是全局
        registry.addInterceptor(new CustomHandlerInterceptor2()).addPathPatterns("/hello/**");
        registry.addInterceptor(new CustomHandlerInterceptor3()).addPathPatterns("/goodbye/**");
    }

    /*@Autowired
    ConfigurableConversionService conversionService;

    //利用生命周期 → 来执行方法
    @PostConstruct
    public void addCustomConverter() {
        conversionService.addConverter(new String2DateConverter());
    }

    @Bean
    @Primary
    public ConfigurableConversionService conversionService() {
        return conversionService;
    }*/

    //添加Converter可以使用addFormatters
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new String2DateConverter());
    }

    @Override
    public Validator getValidator() {
        LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
        validator.setProviderClass(HibernateValidator.class);
        return validator;
    }

    //要么通过方法名指定组件id，要么通过@Bean的value属性指定；组件id是固定值
    @Bean
    public MultipartResolver multipartResolver() {
        return new CommonsMultipartResolver();
    }
}
